package com.example.xy_assistant;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.xy_assistant.bean.Score;
import com.example.xy_assistant.bean.User;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ScoreActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_sno;
    private Button btn_query;
    private OkHttpClient okHttpClient = new OkHttpClient();
    private ListView lv_score;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score);
        initView();
    }

    private void initView() {
        et_sno = (EditText) findViewById(R.id.et_sno);
        btn_query = (Button) findViewById(R.id.btn_query);

        btn_query.setOnClickListener(this);
        lv_score = (ListView) findViewById(R.id.lv_score);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_query:
                submit();
                break;
        }
    }

    private void submit() {
        // validate
        String sno = et_sno.getText().toString().trim();
        if (TextUtils.isEmpty(sno) || sno.length() != 12) {
            Toast.makeText(this, "请输入正确的12位学号", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something
        query();

    }

    private void query() {
        ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("查询中...");
        progressDialog.show();
        Request url = new Request.Builder().url("https://www.ixiyue.cn/test/score.json").build();
        okHttpClient.newCall(url).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                lv_score.post(() -> {
                    Toast.makeText(ScoreActivity.this, "查询失败！", Toast.LENGTH_SHORT).show();
                    progressDialog.dismiss();
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Gson gson = new Gson();
                User user = gson.fromJson(response.body().string(), User.class);
                List<String> scores = new ArrayList<>();
                for (Score score : user.getScore()) {
                    scores.add(score.getName()+"："+score.getScore());
                }
                lv_score.post(() -> {
                    ArrayAdapter adapter = new ArrayAdapter<>(lv_score.getContext(), android.R.layout.simple_expandable_list_item_1, scores);
                    lv_score.setAdapter(adapter);
                    Toast.makeText(ScoreActivity.this, "查询成功", Toast.LENGTH_SHORT).show();
                    progressDialog.dismiss();
                });

            }
        });
    }
}